/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cnguifang.feigewallet.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.cnguifang.feigewallet.DemoHelper;
import com.cnguifang.feigewallet.R;
import com.cnguifang.feigewallet.data.result.SmsCode;
import com.cnguifang.feigewallet.network.AppNetworkUtils;
import com.cnguifang.feigewallet.network.NetworkResultUtils;
import com.cnguifang.feigewallet.utils.AccountValidatorUtil;
import com.hyphenate.exceptions.HyphenateException;
import com.mathum.baseapp.data.BaseData;
import com.mathum.baseapp.network.NetworkCallback;
import com.mathum.baseapp.network.NetworkUtils;

/**
 * register screen
 */
public class ForgetPwdActivity extends BaseActivity {
    private EditText userNameEditText;
    private EditText passwordEditText;
    private EditText confirmPwdEditText;
    private TextView tvCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.em_activity_register1);
        userNameEditText = (EditText) findViewById(R.id.username);
        passwordEditText = (EditText) findViewById(R.id.password);
        confirmPwdEditText = (EditText) findViewById(R.id.sms_code);
        tvCount = (TextView) findViewById(R.id.tv_count);
        tvCount.setOnClickListener(view -> {
            if (TextUtils.isEmpty(userNameEditText.getText().toString())) {
                Toast.makeText(ForgetPwdActivity.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            if (TextUtils.isEmpty(passwordEditText.getText().toString())) {
                Toast.makeText(ForgetPwdActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            if (!AccountValidatorUtil.isMobile(userNameEditText.getText().toString())) {
                Toast.makeText(ForgetPwdActivity.this, "手机格式错误", Toast.LENGTH_LONG).show();
                return;
            }
            tvCount.setText("已发送");
            NetworkUtils.fetchInfo(AppNetworkUtils.initRetrofitApi().fetchSmsCode(userNameEditText.getText().toString()),
                    new NetworkCallback<SmsCode>() {
                        @Override
                        public void onSuccess(SmsCode baseData) {
//                            confirmPwdEditText.setText(baseData.getCodemsg().trim());
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            Toast.makeText(ForgetPwdActivity.this, "网络异常", Toast.LENGTH_LONG).show();
                        }
                    });
        });
    }

    public void register(View view) {
        final String username = userNameEditText.getText().toString().trim();
        final String pwd = passwordEditText.getText().toString().trim();
        String confirm_pwd = confirmPwdEditText.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, getResources().getString(R.string.User_name_cannot_be_empty), Toast.LENGTH_SHORT).show();
            userNameEditText.requestFocus();
            return;
        } else if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, getResources().getString(R.string.Password_cannot_be_empty), Toast.LENGTH_SHORT).show();
            passwordEditText.requestFocus();
            return;
        } else if (TextUtils.isEmpty(confirm_pwd)) {
            Toast.makeText(this, "验证码不能为空!", Toast.LENGTH_SHORT).show();
            confirmPwdEditText.requestFocus();
            return;
        }

        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(pwd)) {
            final ProgressDialog pd = new ProgressDialog(this);
            pd.setMessage("正在重置...");
            pd.show();

            //找回
            Log.e("!1111111111111111", "pwd = " + pwd);
            Log.e("!1111111111111111", "confirm_pwd = " + confirm_pwd);
            NetworkUtils.fetchInfo(AppNetworkUtils.initRetrofitApi().updatePwd(username, pwd, confirm_pwd),
                    new NetworkCallback<BaseData>() {
                        @Override
                        public void onSuccess(BaseData baseData) {
                            if (!NetworkResultUtils.isSuccess(baseData)) {
                                pd.dismiss();
                                Toast.makeText(ForgetPwdActivity.this, baseData.getInfo(), Toast.LENGTH_LONG).show();
                                return;
                            }
                            finish();
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            pd.dismiss();
                            Toast.makeText(ForgetPwdActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });


        }
    }

    public void back(View view) {
        finish();
    }

}
